<?php

/**
 * Disputeo paginator adapter class.
 *
 * @category Zend
 * @package Model
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 */
class Disputeo_Paginator_Adapter extends Zend_Paginator_Adapter_DbSelect
{
    /**
     * @var Disputeo_Model_Table_Row
     */
    protected $_model;
    
    /**
     * @var array
     */
    protected $_models = array();
    
    /**
     * Set the type of object to be returned by paginator
     * 
     * @param \Disputeo_Model_Table_Row $model
     */
    public function setModel(\Disputeo_Model_Table_Row $model)
    {
        $this->_model = $model;
    }
    
    /**
     * Returns an array of items for a page.
     *
     * @param  integer $offset Page offset
     * @param  integer $itemCountPerPage Number of items per page
     * @return array
     */
    public function getItems($offset, $itemCountPerPage)
    {
        if (! $this->_model) {
            return parent::getItems($offset, $itemCountPerPage);
        }
        $this->_select->limit($itemCountPerPage, $offset);
        $rowset = $this->_select->getTable()->fetchAll($this->_select);
        
        $object = get_class($this->_model);
        foreach ($rowset as $row) {
            $data = $row->toArray();
            $model = new $object($data);
            $this->_models[] = $model;
        }

        return $this->_models;
    }

}